package com.cbt.cbtBlog.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class WaterMarkUtil {
   private static final String FONT_NAME = "宋体";
   private static final int FONT_STYLE = 1;
   private static final int FONT_SIZE = 20;
   private static final Color FONT_COLOR;
   private static final String MARK_TEXT = "介绍水印";
   private static final String STRANGE_TEXT = "详情水印";

   public static void textWaterMartA(File pic) {
      FileOutputStream fos = null;

      try {
         Image image = ImageIO.read(pic);
         int width = image.getWidth((ImageObserver)null);
         int height = image.getHeight((ImageObserver)null);
         BufferedImage bufferedImage = new BufferedImage(width, height, 1);
         Graphics2D g = bufferedImage.createGraphics();
         g.drawImage(image, 0, 0, width, height, (ImageObserver)null);
         g.setFont(new Font("宋体", 1, 20));
         g.setColor(FONT_COLOR);
         int x = width - getTextWidth("详情水印") * 20 - 20;
         int y = height - 20;
         g.setComposite(AlphaComposite.getInstance(10, 0.8F));
         g.drawString("详情水印", x, y);
         g.dispose();
         fos = new FileOutputStream(pic.getAbsolutePath());
         ImageIO.write(bufferedImage, "JPEG", fos);
      } catch (FileNotFoundException var19) {
         var19.printStackTrace();
      } catch (IOException var20) {
         var20.printStackTrace();
      } finally {
         if (fos != null) {
            try {
               fos.close();
            } catch (IOException var18) {
               var18.printStackTrace();
            }
         }

      }

   }

   public static void textWaterMartC(File pic, String text) {
      FileOutputStream fos = null;

      try {
         Image image = ImageIO.read(pic);
         int width = image.getWidth((ImageObserver)null);
         int height = image.getHeight((ImageObserver)null);
         BufferedImage bufferedImage = new BufferedImage(width, height, 1);
         Graphics2D g = bufferedImage.createGraphics();
         g.drawImage(image, 0, 0, width, height, (ImageObserver)null);
         g.setFont(new Font("宋体", 1, 20));
         g.setColor(FONT_COLOR);
         int x = width - getTextWidth(text) * 20 - 20;
         int y = height - 20;
         g.setComposite(AlphaComposite.getInstance(10, 0.8F));
         g.drawString(text, x, y);
         g.dispose();
         fos = new FileOutputStream(pic.getAbsolutePath());
         ImageIO.write(bufferedImage, "JPEG", fos);
      } catch (FileNotFoundException var20) {
         var20.printStackTrace();
      } catch (IOException var21) {
         var21.printStackTrace();
      } finally {
         if (fos != null) {
            try {
               fos.close();
            } catch (IOException var19) {
               var19.printStackTrace();
            }
         }

      }

   }

   public static void ImgWaterMart(File pic) {
      FileOutputStream fos = null;

      try {
         Image image = ImageIO.read(pic);
         int width = image.getWidth((ImageObserver)null);
         int height = image.getHeight((ImageObserver)null);
         BufferedImage bufferedImage = new BufferedImage(width, height, 1);
         Graphics2D g = bufferedImage.createGraphics();
         g.drawImage(image, 0, 0, width, height, (ImageObserver)null);
         String localPic = "C:/Users/Administrator/Desktop/images/3.jpg";
         Image localPicMark = ImageIO.read(new File(localPic));
         int localPicMarkWidth = localPicMark.getWidth((ImageObserver)null);
         int localPicMarkHeight = localPicMark.getHeight((ImageObserver)null);
         g.setComposite(AlphaComposite.getInstance(10, 0.8F));
         g.drawImage(localPicMark, (width - localPicMarkWidth) / 2, (height - localPicMarkHeight) / 2, width, height, (ImageObserver)null);
         g.dispose();
         fos = new FileOutputStream(pic.getAbsolutePath());
         ImageIO.write(bufferedImage, "JPEG", fos);
      } catch (FileNotFoundException var21) {
         var21.printStackTrace();
      } catch (IOException var22) {
         var22.printStackTrace();
      } finally {
         if (fos != null) {
            try {
               fos.close();
            } catch (IOException var20) {
               var20.printStackTrace();
            }
         }

      }

   }

   private static int getTextWidth(String text) {
      int length = text.length();
      String temp = null;
      int i = 0;

      for(int len = text.length(); i < len; ++i) {
         temp = String.valueOf(text.charAt(i));
         if (temp.getBytes().length > 1) {
            ++length;
         }
      }

      length = length % 2 == 0 ? length / 2 : length / 2 + 1;
      return length;
   }

   static {
      FONT_COLOR = Color.RED;
   }
}
